package com.ptmall.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ptmall.app.R;


/**
 * Created by MacBook on 17/3/3.
 */

public class TitleBarView extends RelativeLayout {
    private String title, righttitle;   //标题文字
    private Drawable imgright,imgleft;
    private ImageView btnLeft; //左侧按钮控件
    private ImageView btnRight;    //右侧按钮控件
    private TextView tvTitle, tvTitlerights;   //标题文本控件
    private Integer backgroundcolor;
    private boolean setleftimg;
    public TitleBarClickListener listener;
    private RelativeLayout view;

    public TitleBarView(Context context) {
        super(context);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Title_bar);
        init(context, typedArray);
    }


    /**
     * 标题
     *
     * @param str
     */
    public void setText(String str) {
        tvTitle.setText(str);
    }

    /**
     * 右面按钮文字
     *
     * @param str
     */
    public void setRightText(String str) {
        tvTitlerights.setText(str);
        righttitle = str;
        setRightTextVisable(true);
    }

    /**
     * 右面按钮颜色
     *
     * @param color
     */
    public void setRightTextColor(int color) {
        tvTitlerights.setTextColor(color);
    }

    /**
     * 右面按钮能否点击
     *
     * @param pd
     */
    public void setRightTextOnclick(boolean pd) {
        if (pd) {
            tvTitlerights.setPressed(true);
            tvTitlerights.setClickable(true);
            tvTitlerights.setTextColor(getResources().getColor(R.color.white));
        } else {
            tvTitlerights.setPressed(false);
            tvTitlerights.setClickable(false);
            tvTitlerights.setTextColor(getResources().getColor(R.color.white));
        }
    }

    /**
     * 右面按钮能否点击
     *
     * @param pd
     */
    public void setRightTextClickable(boolean pd) {
        if (pd) {
            tvTitlerights.setClickable(true);
        } else {
            tvTitlerights.setClickable(false);
        }
    }

    
    /**
     * 初始化参数，获取属性值
     *
     * @param context 上下文
     * @param ta
     */
    private void init(Context context, TypedArray ta) {
        title = ta.getString(R.styleable.Title_bar_titleText);
        righttitle = ta.getString(R.styleable.Title_bar_righttitleText);
        imgright = ta.getDrawable(R.styleable.Title_bar_rightimg);
        imgleft = ta.getDrawable(R.styleable.Title_bar_leftimg);
        setleftimg = ta.getBoolean(R.styleable.Title_bar_setleftimg, true);
//        backgroundcolor = ta.getColor(R.styleable.Title_bar_backgroundcolor,getResources().getColor(R.color.white));
        ta.recycle();

        LayoutInflater.from(context).inflate(R.layout.titlebar, this);
        view = (RelativeLayout) findViewById(R.id.view);
        btnLeft = (ImageView) findViewById(R.id.btn_titlebar_left);
        btnRight = (ImageView) findViewById(R.id.btn_titlebar_right);
        tvTitle = (TextView) findViewById(R.id.tv_titlebar_name);
        tvTitlerights = (TextView) findViewById(R.id.btn_titlebar_rights);
        if (null != imgright) {
            setRightImgVisable(true);
        }
        if (null != imgleft) {
            btnLeft.setImageDrawable(imgleft);
        }
        if (null != righttitle) {
            setRightTextVisable(true);
        }
//        view.setBackgroundColor(backgroundcolor);

        setLeftBtnVisable(setleftimg);


        tvTitle.setText(title);


        btnLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.leftClick();
                }

            }
        });
        btnRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.rightClick();
                }
            }
        });
        tvTitlerights.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.rightClick();
                }
            }
        });


    }

    /**
     * 定义按钮点击接口，实现回调机制，通过映射的接口对象调用接口中的方法
     * 而不用去考虑如何实现，具体实现由调用者去创建
     */
    public interface TitleBarClickListener {
        void leftClick();   //左侧按钮点击事件

        void rightClick();  //右侧按钮点击事件
    }

    /**
     * 通过接口来获得回调者对接口的实现
     *
     * @param listener
     */
    public void setOnTitleBarClickListener(TitleBarClickListener listener) {
        this.listener = listener;
    }


    /**
     * 设置左侧按钮是否可见
     *
     * @param flag 是否可见
     */
    public void setLeftBtnVisable(boolean flag) {
        if (flag) {
            btnLeft.setVisibility(VISIBLE);
        } else {
            btnLeft.setVisibility(GONE);
        }
    }

    /**
     * 设置右侧按钮是否可见
     *
     * @param flag 是否可见
     */
    public void setRightImgVisable(boolean flag) {
        if (flag) {
            btnRight.setVisibility(VISIBLE);
            btnRight.setImageDrawable(imgright);
        } else {
            btnRight.setVisibility(GONE);
        }
    }

    public void setRightTextVisable(boolean flag) {
        if (flag) {
            tvTitlerights.setVisibility(VISIBLE);
            tvTitlerights.setText(righttitle);
        } else {
            tvTitlerights.setVisibility(GONE);
        }
    }

    public void setRightTitleBg(int background) {
        tvTitlerights.setBackgroundResource(background);
    }

    public TextView getRightTextView() {
        return tvTitlerights;
    }
}
